<?php

namespace app\common\model;

use app\common\pluings\service\Oss;
use think\facade\Db;
use think\model\concern\SoftDelete;

/**
 * Class UserRole
 * @package app\model
 */
class UserRole extends ModelBase
{
    use SoftDelete;
    // 验证器
    protected $validate = \app\common\validate\UserRole::class;
    // 只读不可修改的字段
    protected $readonly = ['id','uuid','client','client_id','uid','create_time','creator'];
    // json 字段定义
    protected $json = ['roles'];
    // 设置JSON数据返回数组
    protected $jsonAssoc = true;

    /**
     * 新增
     */
    public function add($data, $userId){
        empty($data['uuid']) && $data['uuid'] = uuid();
        $data['creator'] = $userId;
        $data['updater'] = $userId;
        validate($this->validate)->scene('add')->check($data);

        $data = $this->createda($data);

        return UserRole::create($data);
    }

    /**
     * 编辑
     */
    public function edit($data, $userId){
        $data['updater'] = $userId;
        validate($this->validate)->scene('edit')->check($data);

        $data = $this->createda($data);

        return UserRole::update($data,['id'=>$data['id'],'uuid'=>$data['uuid']]);
    }

    /**
     * 根据用户id和客户端来更新用户角色信息
     */
    public function editByClient($data, $userId){
        $data['updater'] = $userId;
        validate($this->validate)->scene('edit-by-client')->check($data);

        $data = $this->createda($data);

        return UserRole::where([
            'uid'=>$data['uid'],
            'client'=>$data['client'],
            'client_id'=>$data['client_id']
        ])->find()->save([
            'roles'=>$data['roles'],
            'updater'=>$data['updater']
        ]);
    }

    /**
     * @param $data
     * @param $userId
     * @return bool
     * 删除
     */
    public function del($data, $userId)
    {
        validate($this->validate)->scene('del')->check($data);
        $delFlag = false;
        Db::startTrans();
        try {
            // 先更新再删除
            UserRole::where('id',  $data['id'])
                ->where('uuid', $data['uuid'])
                ->save(['updater' => $userId]);

            $delFlag = UserRole::where(['id'=>$data['id'],'uuid'=>$data['uuid']])->find()->delete();
            // 提交事务
            Db::commit();
        } catch (\Exception $e) {
            // 回滚事务
            Db::rollback();
            exception('删除失败');
        }

        return $delFlag;
    }

    /**
     * @param $data
     * @param $userId
     * @return bool
     * 通过客户端类型和用户id删除用户角色
     */
    public function delByClientAndUid($data, $userId)
    {
        validate($this->validate)->scene('del-by-client-and-uid')->check($data);
        $delFlag = false;
        Db::startTrans();
        try {
            $where = [
                'uid'=>$data['uid']
            ];
            !empty($data['client']) && $where['client'] = $data['client'];
            // 先更新再删除
            UserRole::where($where)
                ->save(['updater' => $userId]);

            $delFlag = UserRole::where(where)->select()->delete();
            // 提交事务
            Db::commit();
        } catch (\Exception $e) {
            // 回滚事务
            Db::rollback();
            exception('删除失败');
        }

        return $delFlag;
    }
}
